import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';
import 'package:x3_business_core/business_common_template/components/business_message_like.dart';

/// author：william on 2021/3/23 11:59
/// email：362661044@qq.com

/// 消息容器
class BusinessMessageContainer extends StatefulWidget {
  final String headImg;
  final String userName;
  final String depotName;
  final String message;
  final String createDate;
  final Widget messageWidget;
  final Widget vest;
  final BusinessMessageLikeModel messageLike;
  const BusinessMessageContainer({
    Key key,
    this.headImg,
    this.userName,
    this.depotName,
    this.message,
    this.messageWidget,
    this.createDate,
    this.vest,
    this.messageLike,
  }) : super(key: key);

  @override
  _BusinessMessageContainerState createState() => _BusinessMessageContainerState();
}

class _BusinessMessageContainerState extends State<BusinessMessageContainer> {
  TextStyle get userNameStyle => Theme.of(context).textTheme.bodyText2.copyWith(fontSize: 14);
  TextStyle get style => Theme.of(context).textTheme.subtitle2;

  @override
  Widget build(BuildContext context) {
    String textImg;
    try {
      textImg = (this.widget.headImg == null || this.widget.headImg.isEmpty) ? widget.userName.substring(0, 1) : null;
    } catch (error) {
      print(error);
    }
    return Stack(
      children: [
        IntrinsicHeight(child: Row(children: [_buildHeadImg(textImg), SizedBox(width: 12), _buildContent()])),
        Positioned(bottom: 0, right: 0, child: Visibility(visible: widget.messageLike != null, child: _buildLiked())),
      ],
    );
  }

  Widget _buildHeadImg(String textImg) {
    return Align(
      alignment: Alignment.topCenter,
      child: BaseImageComponent(imageUrl: this.widget.headImg, placeholderName: textImg, type: BaseImageTypeEnum.X3User, width: 40, height: 40, isClipOval: true),
    );
  }

  Widget _buildContent() {
    String date = BaseTransformUtil.stringToDateTimeStamp(widget.createDate ?? '', isNeedShowSecond: false);
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            BaseComponentUtils.buildTextSpanWidget([this.widget.userName ?? '', this.widget.depotName ?? ''], styles: {0: userNameStyle, 1: style.copyWith(fontSize: 13)}),
            this.widget.vest ?? SizedBox(),
          ],
        ),
        Container(margin: EdgeInsets.symmetric(vertical: 8), child: _buildMessage()),
        Text(date, style: style),
      ],
    );
  }

  Widget _buildMessage() {
    if (widget.messageWidget != null) return widget.messageWidget;
    return Text(this.widget.message ?? '');
  }

  Widget _buildLiked() {
    return BusinessMessageLike(messageLike: widget.messageLike);
  }
}
